<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Program_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getlookup($type){
      $this->db->where('lookup_type',$type);
      return $this->db->get('app_lookup_values')->result();
    }
    
    public function editFormData($data){
      $this->db->query("call app_forms_upd(?,?,?,?,?,?,?,?,?,@err,@msg)",$data);
      $que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
      $id; $msg; $err;
        foreach($que->result() as $keys){
          $msg = $keys->msg;
          $err = $keys->err;
        }
        $arr = array(
            'msg'   =>$msg,
            'err'   =>$err 
        );
      echo json_encode($arr);

    }

    public function insertForm($data){
      $this->db->query("call app_forms_ins(?,?,?,?,?,?,?,?,?,@id,@err,@msg)",$data);
      $que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
      $id; $msg; $err;
        foreach($que->result() as $keys){
          $id = $keys->id;
          $msg = $keys->msg;
          $err = $keys->err;
        }
        $arr = array(
            'modID' =>$id,
            'msg'   =>$msg,
            'err'   =>$err 
        );
      return $arr;
    }

    public function editData($id){

      $this->db->where('form_id',$id);
      $query = $this->db->get('app_forms_v');
      return $query->result();

    }

    public function get_module_name($id){
      return $this->db->query('SELECT SUBSTRING(dir_path,19) dir_path FROM app_modules WHERE module_id = '.$id)->result();
    }

    public function searchFomrData($fcode, $fname, $mname, $efn){
      $select     = "SELECT * FROM app_forms_v";
      $sql;
      $formcode   = "form_code LIKE ".$this->db->escape($fcode);
      $formname   = "form_name LIKE ".$this->db->escape($fname);
      $modulename = "module_name LIKE ".$this->db->escape($mname);
      $exename    = "exe_file_name LIKE ".$this->db->escape($efn);
      if($fcode != "" && $fname == "" && $mname == ""  && $efn == ""){

        $sql = "SELECT * FROM app_forms_v WHERE form_code LIKE '".$fcode."'"; //formcode

      }else if($fcode == "" && $fname == "" && $mname == ""  && $efn != ""){

        $sql = "SELECT * FROM app_forms_v WHERE exe_file_name LIKE '".$efn."'"; //exe file name

      }else if($fcode == "" && $fname == "" && $mname != ""  && $efn == ""){

        $sql = "SELECT * FROM app_forms_v WHERE module_name LIKE '".$mname."'"; //module name

      }else if($fcode == "" && $fname != "" && $mname == ""  && $efn == ""){

        $sql = "SELECT * FROM app_forms_v WHERE form_name LIKE '".$fname."'"; //formname

      }else if($fcode != "" && $fname != "" && $mname == ""  && $efn == ""){

        $sql = "SELECT * FROM app_forms_v WHERE form_name LIKE '".$fname."' AND form_code LIKE '".$fcode."'"; //formcode , formname

      }else if($fcode != "" && $fname != "" && $mname != ""  && $efn == ""){

        $sql = "SELECT * FROM app_forms_v WHERE form_name LIKE '".$fname."' AND module_name LIKE '".$mname."' AND form_name LIKE '".$fname."' AND form_code LIKE '".$fcode."' "; //formcode, formname, module name

      }else if($fcode != "" && $fname != "" && $mname != ""  && $efn != ""){

        $sql = "SELECT * FROM app_forms_v WHERE form_name LIKE '".$fname."' AND module_name LIKE '".$mname."' AND form_code LIKE '".$fcode."' AND exe_file_name LIKE '".$efn."' "; //formcode, formname, module name, exefilename

      }else if($fcode != "" && $fname != "" && $mname == ""  && $efn != ""){

        $sql = "SELECT * FROM app_forms_v WHERE form_name LIKE '".$fname."' AND form_code LIKE '".$fcode."' AND exe_file_name LIKE '".$efn."' "; //formcode, formname, exefilename

      }else if($fcode != "" && $fname == "" && $mname == ""  && $efn != ""){

        $sql = "SELECT * FROM app_forms_v WHERE form_code LIKE '".$fcode."' AND exe_file_name LIKE '".$efn."' "; //formcode exefilename

      }else if($fcode != "" && $fname == "" && $mname != ""  && $efn != ""){

        $sql = "SELECT * FROM app_forms_v WHERE module_name LIKE '".$mname."' AND form_code LIKE '".$fcode."' AND exe_file_name LIKE '".$efn."' "; //formcode, formname, module name, exefilename

      }else if($fcode != "" && $fname == "" && $mname != ""  && $efn == ""){

        $sql = "SELECT * FROM app_forms_v WHERE module_name LIKE '".$mname."' AND form_code LIKE '".$fcode."'"; //formcode, formname, module name, exefilename

      }else if($fcode == "" && $fname != "" && $mname == ""  && $efn != ""){

        $sql = "SELECT * FROM app_forms_v WHERE form_name LIKE '".$fname."' AND exe_file_name LIKE '".$efn."' "; //formcode, formname, module name, exefilename

      }else if($fcode == "" && $fname != "" && $mname != ""  && $efn == ""){

        $sql = "SELECT * FROM app_forms_v WHERE form_name LIKE '".$fname."' AND module_name LIKE '".$mname."' "; //formcode, formname, module name, exefilename

      }
      
      $query = $this->db->query($sql);
      if($query->num_rows() != 0){
        return $query->result();
      }
      else {
        $result = "No results found.";
        return $result;
      }

    }
}